package com.digitalbuana.smiles.data.emoticon;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.provider.BaseColumns;

import com.digitalbuana.smiles.data.DatabaseManager;
import com.digitalbuana.smiles.data.entity.AbstractEntityTable;
import com.digitalbuana.smiles.data.roster.StructuredName;

public class StickerTable extends AbstractEntityTable {

	private static final class Fields implements BaseColumns {

	private Fields() {
		}
		public static final String STICKER_ID = "sticker_id";
		public static final String STICKER_NAME = "sticker_name";
		public static final String STICKER_URL = "sticker_url";
		public static final String STICKER_DESC = "sticker_desc";
		public static final String STICKER_POINT = "sticker_point";
	}
	
	private static final String NAME = "sticker";
	private static final String[] PROJECTION = new String[] {Fields.STICKER_ID, Fields.STICKER_NAME,
			Fields.STICKER_URL, Fields.STICKER_DESC, Fields.STICKER_URL};
	
	private final DatabaseManager databaseManager;
	private SQLiteStatement writeStatement;
	private final Object writeLock;

	private final static StickerTable instance;

	static {
		instance = new StickerTable(DatabaseManager.getInstance());
		DatabaseManager.getInstance().addTable(instance);
	}
	
	public static StickerTable getInstance() {
		return instance;
	}

	private StickerTable(DatabaseManager databaseManager) {
		this.databaseManager = databaseManager;
		writeStatement = null;
		writeLock = new Object();
	}
	
	
	void write(StructuredName name) {
		synchronized (writeLock) {
			if (writeStatement == null) {
				SQLiteDatabase db = databaseManager.getWritableDatabase();
				writeStatement =
						db.compileStatement("INSERT OR REPLACE INTO "
						+ NAME + " ("
						+ Fields.STICKER_ID + ", "
						+ Fields.STICKER_NAME + ", "
						+ Fields.STICKER_URL + ", "
						+ Fields.STICKER_DESC + ", "
						+ Fields.STICKER_POINT+ ") VALUES (?, ?, ?, ?, ?);");
			}
			writeStatement.bindString(1, name.getBestName());
			writeStatement.bindString(2, name.getNickName());
			writeStatement.bindString(3, name.getFormattedName());
			writeStatement.bindString(4, name.getFirstName());
			writeStatement.bindString(5, name.getMiddleName());
			writeStatement.execute();
		}
	}
	
	@Override
	public void create(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + NAME + " (" + Fields.STICKER_ID
				+ " TEXT PRIMARY KEY," + Fields.STICKER_NAME + " TEXT,"
				+ Fields.STICKER_URL + " TEXT," + Fields.STICKER_DESC
				+ " TEXT," + Fields.STICKER_POINT + " TEXT);";
		DatabaseManager.execSQL(db, sql);
	}
	
	static String getID(Cursor cursor) {
		return cursor.getString(cursor.getColumnIndex(Fields.STICKER_ID));
	}
	static String getName(Cursor cursor) {
		return cursor.getString(cursor.getColumnIndex(Fields.STICKER_NAME));
	}
	static String getStickerURL(Cursor cursor) {
		return cursor.getString(cursor.getColumnIndex(Fields.STICKER_URL));
	}
	static String geStickerDesc(Cursor cursor) {
		return cursor.getString(cursor.getColumnIndex(Fields.STICKER_DESC));
	}
	static String getStickerPoint(Cursor cursor) {
		return cursor.getString(cursor.getColumnIndex(Fields.STICKER_POINT));
	}

	@Override
	protected String getTableName() {
		return NAME;
	}

	@Override
	protected String[] getProjection() {
		return PROJECTION;
	}

}
